﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;


namespace BasicFrameworkDemo.WCFInstaller
{
    using System.IO;

    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
        }
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            this.Create();
        }

        
        public override void Rollback(IDictionary savedState)
        {
            if (savedState == null)
            {
                throw new ApplicationException("Rollback Error.");
            }

            base.Rollback(savedState);
            this.Delete();
        }

        public override void Uninstall(IDictionary savedState)
        {
            if (savedState == null)
            {
                throw new ApplicationException("Uninstall Error.");
            }

            base.Uninstall(savedState);
            this.Delete();
        }

        private void Create()
        {
            // name=[SITENAME] /port=[PORT] /dataserver=[DBSERVER]  /dataname=[DBNAME] /datauser=[DBUSERNAME] /datapassword=[DBPASSWORD] /targetdir="[TARGETDIR]""
            var defaultVrootPath = this.Context.Parameters["targetdir"];
            var name = this.Context.Parameters["name"];
            var port = this.Context.Parameters["port"];
            var dataserver = this.Context.Parameters["dataserver"];
            var datauser = this.Context.Parameters["datauser"];
            var datapassword = this.Context.Parameters["datapassword"];
            var dataname = this.Context.Parameters["dataname"];

            if (string.IsNullOrEmpty(defaultVrootPath) || string.IsNullOrWhiteSpace(defaultVrootPath))
            {
                return;
            }

            defaultVrootPath = defaultVrootPath.TrimEnd('"', ' ');

            InstallConfiguration.AddDirectoryAccountSecurity(defaultVrootPath);
            InstallConfiguration.CreateWebSite(name, port, defaultVrootPath, true);
            InstallConfiguration.AddExtensionlessUrl(name);

            var dictionary = new Dictionary<string, string>
                                 {
                                     { "AppSettingTest", "MyAppSetting" },
                                 };
            foreach (var key in dictionary.Keys)
            {
                InstallConfiguration.AddAppSettings(name, key, dictionary[key]);
            }

            var connectionString = string.Format("Database={0};User ID={1};Password={2};Application Name=AOTT-GateWay; Server={3};Connect Timeout=30;", dataname, datauser, datapassword, dataserver);
            InstallConfiguration.AddConnectionStrings(name, "connectionStrings", connectionString, "System.Data.SqlClient");
        }

        
        private void Delete()
        {
            // name=[NAME] /port=[PORT] /targetdir="[TARGETDIR]""
            var defaultVrootPath = this.Context.Parameters["targetdir"];
            var name = this.Context.Parameters["name"];
            var port = this.Context.Parameters["port"];

            if (string.IsNullOrEmpty(defaultVrootPath) || string.IsNullOrWhiteSpace(defaultVrootPath))
            {
                return;
            }

            defaultVrootPath = defaultVrootPath.TrimEnd('"', ' ');

            InstallConfiguration.DeleteWebSite(name, port);

            try
            {
                if (Directory.Exists(defaultVrootPath))
                {
                    Directory.Delete(defaultVrootPath, true);
                }
            }
            catch
            {
                return;
            }
        }
    }
}
